package com.financing.wap.web.controller.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.common.Session;
import com.financing.wap.dto.FpVideoDTO;
import com.financing.wap.dto.FpVideoHisDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.service.IFpVideoHisService;
import com.financing.wap.service.IFpVideoService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.web.controller.BaseController;

@Controller("fpVideoController")
@RequestMapping("/api/fpVideo")
public class FpVideoController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IFpVideoService fpVideoService;
	
	@Autowired
	private IFpVideoHisService fpVideoHisService;
	@RequestMapping("/listFpVideo")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, FpVideoDTO fpVideo,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query fpVideo list begin .......");
			
			fpVideo.setBegin(pager.getStartIndex());
			fpVideo.setPageSize(pager.getPageSize());
			
			int count = fpVideoService.queryListCount(fpVideo);
			
			pager.setTotalCount(count);
			
			List<FpVideoDTO> fpVideoList = null;
			
			if(count > 0){
				fpVideoList = fpVideoService.queryList(fpVideo);
			}else{
				fpVideoList = Collections.emptyList();
			}
			
			
			/*Session session = getSession(true);
			UserDTO loginUser = session.getUser();
			//未登录将密码清空
			if(loginUser==null){
				for (FpVideoDTO bean: fpVideoList) {
					bean.setEncryptionPassword(null);
				}
			}*/
			
			result.setAttribute("pager", pager);
			result.setAttribute("list", fpVideoList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query fpVideo list end .......");
		}catch(Exception e){
			logger.error("query fpVideo error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailFpVideo")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, FpVideoDTO fpVideo){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail fpVideo  begin .......");
			
			Integer id = fpVideo.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			FpVideoDTO dto = fpVideoService.loadById(id);
			
			/*Session session = getSession(true);
			UserDTO loginUser = session.getUser();
			//未登录将密码清空
			if(loginUser==null){
				dto.setEncryptionPassword(null);
			}*/
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail fpVideo  end .......");
		}catch(Exception e){
			logger.error("query detail fpVideo error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	
	@RequestMapping("/playVideo")
	@ResponseBody
	public synchronized ResultMap playVideo(HttpServletRequest request, FpVideoDTO fpVideo){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("play Video  begin .......");
			
			Integer id = fpVideo.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			FpVideoDTO dto = fpVideoService.loadById(id);
			Integer actualBrowseVolume = dto.getActualBrowseVolume()==null?0:dto.getActualBrowseVolume();
			Integer browseVolume = dto.getBrowseVolume()==null?0:dto.getBrowseVolume();
			FpVideoDTO update =  new FpVideoDTO();
			update.setId(id);
			update.setActualBrowseVolume(actualBrowseVolume+1);
			update.setBrowseVolume(browseVolume+1);
			fpVideoService.update(update);
			
			FpVideoHisDTO fpVideoHis = new FpVideoHisDTO();
			fpVideoHis.setVideoId(id);
			fpVideoHis.setBrowsePyte("wap");
			fpVideoHis.setDeleteTag("1");
			DTOdateSetUtil.setUpdateInfo(fpVideoHis);
			DTOdateSetUtil.setCreateInfo(fpVideoHis);
			fpVideoHisService.insert(fpVideoHis);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("play Video  end .......");
		}catch(Exception e){
			logger.error("play Video error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
}
